package com.hsh.server.project.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hsh.server.constant.CustomException;
import com.hsh.server.constant.HttpStatus;
import com.hsh.server.util.Convert;
import com.hsh.server.util.DateUtils;
import com.hsh.server.util.R;
import com.hsh.server.util.StringUtils;
import com.hsh.server.util.redis.JwtWithHutoolExample;
import com.hsh.server.util.redis.RedisConstants;
import com.hsh.server.util.redis.TokenServiceTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hsh.server.project.user.mapper.TAppUserMapper;
import com.hsh.server.project.user.entity.TAppUser;
import com.hsh.server.project.user.service.ITAppUserService;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author hsh
 * @date 2025-03-27
 */
@Service
public class TAppUserServiceImpl implements ITAppUserService 
{
    @Autowired
    private TAppUserMapper tAppUserMapper;
    @Autowired
    private TokenServiceTask tokenServiceTask;
    /**
     * 查询【请填写功能名称】
     * 
     * @param userId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TAppUser selectTAppUserById(Long userId)
    {
        return tAppUserMapper.selectTAppUserById(userId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tAppUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TAppUser> selectTAppUserList(TAppUser tAppUser)
    {
        return tAppUserMapper.selectTAppUserList(tAppUser);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tAppUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public R insertTAppUser(TAppUser tAppUser)
    {
        if (StringUtils.isEmpty(tAppUser.getUserAccount())||StringUtils.isEmpty(tAppUser.getUserPassword())) {
            return R.error("账号或密码不能为空");
        }
        tAppUser.setCreateTime(DateUtils.getNowDate());

        tAppUserMapper.insertTAppUser(tAppUser);
        return R.success("注册成功");
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tAppUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTAppUser(TAppUser tAppUser)
    {
        return tAppUserMapper.updateTAppUser(tAppUser);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTAppUserByIds(String ids)
    {
        return tAppUserMapper.deleteTAppUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param userId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTAppUserById(Long userId)
    {
        return tAppUserMapper.deleteTAppUserById(userId);
    }

    @Override
    public R appLogin(TAppUser tAppUser) {
        if (StringUtils.isEmpty(tAppUser.getUserAccount())||StringUtils.isEmpty(tAppUser.getUserPassword())) {
            return R.error("账号或密码不能为空");
        }
        //效验账号是否存在
        TAppUser account =  tAppUserMapper.selectTAppUserByAccount(tAppUser);
        if (account==null) {
            return R.error("用户账号不存在，请注册");
        }

        //判断密码是否正确
        if (!account.getUserPassword().equals(tAppUser.getUserPassword())) {
            return R.error("密码错误");
        }
        String token = JwtWithHutoolExample.generateJwt(account.getUserId()+"");
        // 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();
        String  jsonAccount ="";
        try {
             jsonAccount = objectMapper.writeValueAsString(account);
        } catch (
                JsonProcessingException e) {
            e.printStackTrace();
            throw new CustomException("转换对象错误", HttpStatus.ERROR);
        }

        tokenServiceTask.setTokenWithTime(RedisConstants.LOGIN_USER_KEY+ account.getUserId(), jsonAccount, 43200, TimeUnit.MINUTES);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("token", token);
        map.put("userInfo", account);
        return R.success("登陆成功",map);
    }
}
